package bairui.international.weixin.portal.miniapp.controller.branchCompany.staff;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.lianjing.sh.validate.spring.exception.ValidateObjectException;

import bairui.international.model.domain.jpa.TB_BranchCompanyStaff;
import bairui.international.model.domain.jpa.TB_BranchCompanyStaffWithdrawOrder;
import bairui.international.model.domain.jpa.TB_BranchCompanyWithdrawOrder;
import bairui.international.model.domain.jpa.TB_SystemConfig;
import bairui.international.model.domain.jpa.TB_User;
import bairui.international.service.Service_BranchCompanyStaff;
import bairui.international.service.Service_BranchCompanyStaffWithdrawOrder;
import bairui.international.service.Service_BranchCompanyWithdrawOrder;
import bairui.international.service.Service_SystemConfig;
import bairui.international.service.Service_User;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

/**
 *
 * 提现
 *
 * @generated
 * 
 * @see STest_C_BranchCompnay_Staff_Withdraw_Post 测试
 *
 */
@Controller
public class C_BranchCompnay_Staff_Withdraw_Post
 extends SC_BranchCompnay_Staff_Withdraw_Post 
{
	
	@Autowired private Service_BranchCompanyWithdrawOrder service_BranchCompanyWithdrawOrder;
	@Autowired private Service_BranchCompanyStaffWithdrawOrder service_BranchCompanyStaffWithdrawOrder;
	@Autowired private Service_User service_User;
	@Autowired private Service_BranchCompanyStaff service_BranchCompanyStaff;
	@Autowired private Service_SystemConfig service_SystemConfig;
	
	/**
	 * 申请
	 */
	@Action(Post.class)
	@PostMapping(Post.RequestMappingValue)
	public @FomatOutput @ResponseBody Object post(
		ModelMap map, HttpServletRequest request,
		@RequestParam String skey,
		@RequestBody Input input
	) throws Exception {
		TB_User user = service_User.findOne(it->it.skey.eq(skey));
		TB_BranchCompanyStaff branchCompanyStaff = service_BranchCompanyStaff.getActive(user);
		TB_SystemConfig bean = service_SystemConfig.findFirst();
		if(input.getAmount() < bean.getWithdrawMinAmount()){
			throw new ValidateObjectException("object", "", "提现金额不能小于最小提现额度");
		}
		// 创建订单
		if(branchCompanyStaff.getDirectorFlag()){
			// 验证
			if(input.getAmount() > branchCompanyStaff.getBranchCompany().getWithdrawMoney()){
				throw new ValidateObjectException("object", "", "提现金额不能大于账户余额");
			}
			TB_BranchCompanyWithdrawOrder order = service_BranchCompanyWithdrawOrder.create(branchCompanyStaff, input.getAmount());
			return new Output(order.getId());
		}else{
			// 验证
			if(input.getAmount() > branchCompanyStaff.getWithdrawMoney()){
				throw new ValidateObjectException("object", "", "提现金额不能大于账户余额");
			}
			TB_BranchCompanyStaffWithdrawOrder order = service_BranchCompanyStaffWithdrawOrder.create(branchCompanyStaff, input.getAmount());
			return new Output(order.getId());
		}
	}
	
	public static class Input {
		private @Getter @Setter Double amount;
	}
	
	@AllArgsConstructor
	public static class Output {
		private @Getter Long id;
	}

}
